<?php if (!defined('THINK_PATH')) exit(); /*a:1:{s:76:"/Users/yandian/work/matian/application/admin/view/bootstrap/weixin/menu.html";i:1477908548;}*/ ?>
<div class="easyui-layout" data-options="fit:true">
    <div data-options="region:'west'" title="微信菜单列表"
         style="width:200px;padding:10px">
        <ul id="menu_tree" class="easyui-tree"
            data-options="url:'<?php echo U(CONTROLLER_NAME.'/getMenusByPId',array('root'=>1)); ?>'"></ul>
    </div>
    <div data-options="region:'center'">
        <table id="menu_tree_table" title="菜单详细列表"
               data-options="fit:true,pagination:true,border:false,
               singleSelect:false,collapsible:false,method:'get',sortName:'sort desc',sortOrder:',id asc'">
            <thead>
            <tr>
                <th data-options="field:'id',width:80,checkbox:true"></th>
                <th data-options="sortable:true,field:'title',width:120">标题</th>
                <th data-options="sortable:true,field:'parent_name',width:120">父菜单</th>
                <th data-options="sortable:true,field:'type',width:180,align:'left'">菜单的动作类型</th>
                <th data-options="sortable:true,field:'key',width:180,align:'left'">菜单KEY值</th>
                <th data-options="sortable:true,field:'media_id',width:180,align:'left'">永久素材ID</th>
                <th data-options="sortable:true,field:'url',width:180,align:'left'">url地址</th>
                <th data-options="sortable:true,field:'is_auth',width:180,align:'left',formatter:_Formatter.YesOrNo">微信认证菜单</th>
                <th data-options="sortable:true,field:'is_valid',width:80,formatter:function(v){
                        if(v == 1){
                            return '<span style=\'color:green;font-weight:bold\'>有效</span>';
                        }
                        return '<span style=\'color:red;font-weight:bold\'>无效</span>';
                    }">是否有效
                </th>
                <th data-options="sortable:true,field:'sort',width:80">排序</th>
                <th data-options="sortable:true,field:'create_time',width:130">创建时间</th>
                <th data-options="sortable:true,field:'creator',width:80">创建者</th>
                <th data-options="sortable:true,field:'modify_time',width:130">修改时间</th>
                <th data-options="sortable:true,field:'modifier',width:80">修改人</th>
            </tr>
            </thead>
        </table>
    </div>
</div>

<div id="dlg" class="easyui-dialog" title="添加记录"
     style="width:550px;height:450px;padding:20px 50px"
     data-options="
                closed:true,
                modal:true,
				buttons: [{
					text:'确定',
					ajax:true,
					iconCls:'icon-ok',
					handler:function(){
                        $('#ff').form('submit',{
                            onSubmit:function(){
                                return $(this).form('enableValidation').form('validate');
                            },
                            success:function(data){
                                if(typeof data == 'string'){
                                    data = $.parseJSON(data);
                                }
                                if(data.status){
                                     $('#menu_tree').tree('reload');
                                     $('#parent_id').combotree('reload');
                                     _Func.getMenu();
                                     $('#menu_tree_table').datagrid('reload');
                                     $('#dlg').dialog('close');
                                     $.messager.show({
                                        title:'提示',
                                        content: '操作成功'
                                     });
                                }
                            }
			            });
					}
				},{
					text:'取消',
					iconCls:'icon-cancel',
					handler:function(){
					    $('#dlg').dialog('close');
					}
				}]
			">
    <form id="ff" action="<?php echo U(CONTROLLER_NAME.'/saveEntry'); ?>" class="easyui-form" method="post"
          data-options="novalidate:true">
        <input type="hidden" name="id"/>
        <table cellpadding="5">
            <tr>
                <td>菜单名称:</td>
                <td><input class="easyui-textbox" type="text" name="title" style="width:200px;" data-options="required:true"/></td>
            </tr>
            <tr>
                <td>父菜单:</td>
                <td><input class="easyui-combotree" id="parent_id" type="text" name="parent_id" style="width:200px;"
                           data-options="required:false,url:'<?php echo U('getMenusByPId',array('root'=>1)); ?>'"/></td>
            </tr>
            <tr>
                <td>动作类型(type):</td>
                <td>
                    <select name="type" id="type" style="width:172px;" data-options="editable:false,panelHeight:'auto'">
                        <option value="view">view</option>
                        <option value="click">click</option>
                        <option value="media_id">media_id</option>
                        <option value="view_limited">view_limited</option>
                    </select>
                </td>
            </tr>
            <tr id="key">
                <td>键值(key):</td>
                <td><input class="easyui-textbox" type="text" name="key" data-options="required:false"/></td>
            </tr>
            <tr id="media_id">
                <td>素材ID(media_id):</td>
                <td><input class="easyui-textbox" type="text" name="media_id"
                           data-options="required:false"/></td>
            </tr>
            <tr id="url">
                <td>菜单地址:</td>
                <td><input class="easyui-textbox" type="text" name="url" style="width: 300px;" data-options="required:false"/></td>
            </tr>
            <tr>
                <td>排序号:</td>
                <td>
                    <input class="easyui-numberbox" id="sort" type="text" name="sort" data-options="required:false"/>
                </td>
            </tr>
            <tr>
                <td>是否有效:</td>
                <td>
                    <input type="hidden" name="is_valid" value="0"/>
                    <input type="checkbox" id="is_valid" name="is_valid" value="1"/>
                </td>
            </tr>
            <tr>
                <td>是否认证菜单:</td>
                <td>
                    <input type="hidden" name="is_auth" value="0"/>
                    <input type="checkbox" id="is_auth" name="is_auth" value="1"/>
                </td>
            </tr>
        </table>
    </form>
</div>


<script type="text/javascript">
    (function () {
        var dg = $('#menu_tree_table').datagrid({
            url: "<?php echo U('find'); ?>",
            method: 'post',
            toolbar: [
                {
                    text: '添加',
                    iconCls: 'icon-add',
                    handler: function () {
                        hideInputs();
                        $('#ff').form('clear');
                        var row = $('#menu_tree').tree('getSelected');
                        if (row) {
                            $('#parent_id').combotree('setValue', row.id);
                        }
                        $('#dlg').dialog({
                            title: '添加记录'
                        }).dialog('open');
                        $('#sort').numberbox('setValue', 0);
                        $('#is_valid')[0].checked = true;
                    }
                },
                {
                    text: '修改',
                    iconCls: 'icon-edit',
                    handler: function () {
                        hideInputs();
                        var rows = $('#menu_tree_table').datagrid('getSelections');
                        if (!rows || rows.length !== 1) {
                            $.messager.alert('提示', '请选择一条记录！');
                            return;
                        }
                        $('#ff').form('clear').form('load', rows[0]);
                        $('#is_valid')[0].checked = (rows[0].is_valid == '1');
                        $('#dlg').dialog({
                            title: '修改记录'
                        }).dialog('open');
                    }
                },
                {
                    text: '删除',
                    iconCls: 'icon-remove',
                    handler: function () {
                        var rows = $('#menu_tree_table').datagrid('getSelections');
                        if (!rows || !rows.length) {
                            $.messager.alert('提示', '请选择记录！');
                        } else {
                            $.messager.confirm('提示', '确定删除所选记录吗？', function (r) {
                                if (r) {
                                    var ids = [];
                                    for (var i = 0, l = rows.length; i < l; i++) {
                                        ids.push(rows[i].id);
                                    }
                                    $.post("<?php echo U(CONTROLLER_NAME.'/deleteModel'); ?>", {
                                        ids: ids.join(',')
                                    }, function (data) {
                                        if (typeof data == 'string') {
                                            data = $.parseJSON(data);
                                        }
                                        if (data.status) {
                                            $('#menu_tree').tree('reload');
                                            $('#parent_id').combotree('reload');
                                            _Func.getMenu();
                                            $('#menu_tree_table').datagrid('reload');
                                            $('#dlg').dialog('close');
                                            $.messager.show({
                                                title: '提示',
                                                content: '操作成功'
                                            });
                                        }
                                    });
                                }
                            });
                        }
                    }
                },
                {
                    text: '提交菜单',
                    iconCls: 'icon-save',
                    handler: function () {
                        _Func.busy.show();
                        $.get("<?php echo U('submitMenu'); ?>", function (data) {
                            _Func.busy.hide();
                            try {
                                if (data.status) {
                                    _Func.messager.show('提交菜单成功！');
                                } else {
                                    alert(data.info);
                                }
                            } catch (e) {
                                alert(e.message)
                            }

                        }, 'json');
                    }
                }
            ],
            onBeforeLoad: function () {
                if (!arguments.callee.loaded) {
                    arguments.callee.loaded = true;
                    return false;
                }
                return true;
            },
            data: []
        });
        var filters = [{
            field: 'is_valid',
            type: 'combobox',
            options: {
                panelHeight: 'auto',
                valueField: 'id',
                textField: 'text',
                editable: false,
                data: [
                    {
                        id: '',
                        text: '全部'
                    },
                    {
                        id: '1',
                        text: '是'
                    },
                    {
                        id: '0',
                        text: '否'
                    }
                ],
                onChange: function (newValue, oldValue) {
                    if (newValue) {
                        dg.datagrid('addFilterRule', {
                            field: 'is_valid',
                            value: newValue
                        }).datagrid('doFilter');
                    } else {
                        dg.datagrid('removeFilterRule', 'is_valid').datagrid('doFilter');
                    }
                }
            }
        }];
        $('#menu_tree').tree({
            onClick: function (node) {
                dg.datagrid('removeFilterRule');
                dg.datagrid({
                    queryParams: {
                        pid: node.id
                    }
                });
                dg.datagrid('enableFilter', filters);
            }
        });
        dg.datagrid('enableFilter', filters);

        $('#type').combobox({
            onChange: function (v) {
                switch (v) {
                    case 'view':
                        showInput('url');
                        break;
                    case 'click':
                        showInput('key');
                        break;
                    case 'media_id':
                    case 'view_limited':
                        showInput('media_id');
                        break;
                }
            }
        });

        var fieldIds = ['key', 'url', 'media_id', 'view_limited'];

        function showInput($exceptField) {
            for (var i = 0, c; c = fieldIds[i++];) {
                if (c != $exceptField) {
                    $('#' + c).hide();
                } else {
                    $('#' + c).show();
                }
            }

        }

        function hideInputs() {
            for (var i = 0, c; c = fieldIds[i++];) {
                $('#' + c).hide();
            }
        }
    })();
</script>
